/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.ventilation.code.utils.WebSocket;

import android.app.Activity;
import android.content.Context;

import com.example.ventilation.code.utils.SharePrefrenceUtil;
import com.example.ventilation.code.utils.UrlConstant;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * 这个类管理WsManager的 -- 创建、销毁
 */
public class WebSocketManager {
    private  static  WsManager manager;
    public static void initSocket(final Activity activity){//mainActivity内初始化
        if (manager != null) {
            manager.stopConnect();
            manager = null;
        }
        SharePrefrenceUtil sharePrefrenceUtil = new SharePrefrenceUtil(activity);
        String url = UrlConstant.WEBSOCKET_HOST+sharePrefrenceUtil.getUdid()+"/"+sharePrefrenceUtil.getToken();
        manager = new WsManager.Builder(activity).client(
                new OkHttpClient().newBuilder()
                        .pingInterval(15, TimeUnit.SECONDS)
                        .retryOnConnectionFailure(true)
                        .build())
                .needReconnect(true)
                .wsUrl(url)
                .build();
    }
    public static WsManager getWsManager(){
        return manager;
    }
//    public static WsManager initSocket(WsManager wsManager,final Activity activity){
//        if (wsManager != null) {
//            wsManager.stopConnect();
//            wsManager = null;
//        }
//        SharePrefrenceUtil sharePrefrenceUtil = new SharePrefrenceUtil(activity);
//        String url = UrlConstant.WEBSOCKET_HOST+sharePrefrenceUtil.getUdid()+"/"+sharePrefrenceUtil.getToken();
//        wsManager = new WsManager.Builder(activity).client(
//                new OkHttpClient().newBuilder()
//                        .pingInterval(15, TimeUnit.SECONDS)
//                        .retryOnConnectionFailure(true)
//                        .build())
//                .needReconnect(true)
//                .wsUrl(url)
//                .build();
//        return wsManager;
//    }
    public static void close(){
        if (manager != null) {
            manager.stopConnect();
//            wsManager = null;
        }
    }
    public static void destory(WsManager wsManager){
        if (manager != null) {
            manager.stopConnect();
           manager = null;
        }
    }

    public  static  WsManager1 newInstance(Context context){
        SharePrefrenceUtil sharePrefrenceUtil = new SharePrefrenceUtil(context);
        String url = UrlConstant.WEBSOCKET_HOST+sharePrefrenceUtil.getUdid()+"/"+sharePrefrenceUtil.getToken();
        WsManager1 wsManager = new WsManager1.Builder(context)
                .client ( new OkHttpClient().newBuilder()
                        .pingInterval(15, TimeUnit.SECONDS)
                        .retryOnConnectionFailure(true).build())
                .needReconnect(true)
                .wsUrl(url)
                .build();
        return wsManager;
    }
}
